﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Text;

namespace KBEngine.Common
{
    public class ComponentInfo
    {
        public ulong entities = 0;    //KBEngine.Entity数量
        public ulong clients = 0;     //客户端数量
        public ulong proxies = 0;     //KBEngine.Proxy实例数量
        public ulong consolePort = 0; //控制台端口
        public int genuuid_sections = 0; // --gus

        public IPAddress intaddr;
        public ushort intport;
        public IPAddress extaddr;
        public ushort extport;
        public string extaddrEx;
        public uint pid;
        public float cpu;
        public float mem;
        public uint usedmem;
        public sbyte state;
        public uint machineID;
        public ulong extradata;
        public ulong extradata1;
        public ulong extradata2;
        public ulong extradata3;
        public uint backaddr;
        public ushort backport;
        public int uid;
        public string username;
        public ComponentType componentType;
        public ulong componentID;
        public ulong componentIDEx;
        public int globalOrderID;
        public int groupOrderID;

        public string fullname;

        public ComponentInfo(MemoryStream memoryStream = null)
        {
            if (memoryStream != null)
                initFromStream(memoryStream);
        }
        private void initFromStream(MemoryStream reader)
        {
            uid = reader.readInt32();
            username = reader.readString();
            componentType = (ComponentType)reader.readInt32();
            componentID = reader.readUint64();
            componentIDEx = reader.readUint64();
            globalOrderID = reader.readInt32();
            groupOrderID = reader.readInt32();
            genuuid_sections = reader.readInt32();

            intaddr = new IPAddress(reader.readUint32());
            intport = (ushort)IPAddress.NetworkToHostOrder((short)reader.readUint16());
            extaddr = new IPAddress(reader.readUint32());
            extport = (ushort)IPAddress.NetworkToHostOrder((short)reader.readUint16());
            extaddrEx = reader.readString();
            pid = reader.readUint32();
            cpu = reader.readFloat();
            mem = reader.readFloat();
            usedmem = reader.readUint32();
            state = reader.readInt8();
            machineID = reader.readUint32();
            extradata = reader.readUint64();
            extradata1 = reader.readUint64();
            extradata2 = reader.readUint64();
            extradata3 = reader.readUint64();
            backaddr = reader.readUint32();
            backport = reader.readUint16();

            consolePort = extradata3;
            if (componentType == ComponentType.baseapp || componentType == ComponentType.cellapp)
                fullname = componentType.ToString() + groupOrderID.ToString();
            else
                fullname = componentType.ToString();

            if (componentType == ComponentType.baseapp || componentType == ComponentType.cellapp)
                entities = extradata;

            if (componentType == ComponentType.baseapp)
                clients = extradata1;

            if (componentType == ComponentType.baseapp)
                proxies = extradata2;
        }
    }
}
